 var rounding=function (count,times,bit) {
 	return parseFloat(Number(count*times).toFixed(bit));
 }
 var myChart;var bmap; var bmaps;
 var detailsInf=function (data) {
   var rectangleAll=[],cellcolorAll=[];
   console.log(data);
   var mediapage = data.rows.MediaPage;
   myChart= echarts.init(document.getElementById('map'));
   var startPoint=null;
   if (sessionStorage.getItem('fnid')) {
     startPoint= {
      x:mediapage.grid_list[0].center_bd_lng,
      y:mediapage.grid_list[0].center_bd_lat
    }
    $('.back').append('<i class="planBackIcon"></i><span class="planBack">方案详情</span>');
    $('.media_title').html('栅格'+mediapage.grid_list[0].fnidName);
  }else if(sessionStorage.getItem('coordIng')&&sessionStorage.getItem('coordLat')){
    startPoint={
      x:sessionStorage.getItem('coordIng'),
      y:sessionStorage.getItem('coordLat')
    }
  }else{
    startPoint = {
      x: 116.406281,
      y: 39.916485
    };
    $('.back .planBack').remove();
    $('.back .planBackIcon').remove();
  }
  bmap= {
    center: [startPoint.x, startPoint.y],
    zoom: 12,
    roam: 'move',
    mapStyle: {
      styleJson: [
      {
        "featureType": "background",
        "elementType": "all",
        "stylers": {
          "color": "#2d3039"
        }
      },
      {
        "featureType": "poi",
        "elementType": "labels.icon",
        "stylers": {
          "color": "#3d85c6"
        }
      },
      {
        "featureType": "highway",
        "elementType": "all",
        "stylers": {
          "visibility": "off"
        }
      },
      {
        "featureType": "local",
        "elementType": "all",
        "stylers": {
          "lightness": -76,
          "saturation": -77
        }
      },
      {
        "featureType": "arterial",
        "elementType": "all",
        "stylers": {
          "color": "#d0e0e3",
          "lightness": -73,
          "saturation": -77
        }
      }
      ]
    },
  };
  if (sessionStorage.getItem('fnid')) {
    bmap.zoom=15;
  }else if (sessionStorage.getItem('zoom')) {
    bmap.zoom=sessionStorage.getItem('zoom');
  }
  var color = ['#fff'];
  var series = [];
  series.push({
    type: 'custom',
    coordinateSystem: 'bmap',
    animation: false,
    itemStyle: {
      emphasis: {
        color: 'yellow'
      }
    },
    encode: {
      tooltip: 2
    },
  })
  myChart.on("click", function (param){
    console.log(param)
  });
  option = {
    bmap: bmap,
    color: ['gold', 'aqua', 'lime'],
    backgroundColor: '#404a59',

    tooltip: {
      trigger: 'item'
    },
    geo: {
      map: 'bmap',

      polyline: true,
      progressiveThreshold: 500,
      progressive: 200,
      label: {
        emphasis: {
          show: false
        }
      },
      roam: true,
      itemStyle: {
        normal: {
          areaColor: '#323c48',
          borderColor: '#404a59'
        },
        emphasis: {
          areaColor: '#2a333d'
        }
      }
    },
    series: series
  };
  myChart.setOption(option);
 bmaps = myChart.getModel().getComponent('bmap').getBMap();
  $('.databtn_wrp a').on('click',function () {
    sessionStorage.setItem('coordIng',bmaps.getCenter().lng);
    sessionStorage.setItem('coordLat',bmaps.getCenter().lat);
    sessionStorage.setItem('zoom',bmap.zoom);
    // console.log(bmap.zoom);
  })
  
  var maxX_bd=[],maxY_bd=[],minX_bd=[],minY_bd=[],cellcolor=[];

  var allcellOverlay = bmaps.getOverlays();

  for (var i = 0; i < allcellOverlay.length; i++) {
    if(allcellOverlay[i].Qa==true){
      console.log(allcellOverlay[i]);
      bmaps.removeOverlay(allcellOverlay[i]);
    }
  }
  bmaps.setMinZoom(12);
  for(var k in mediapage.grid_list){
   maxX_bd=mediapage.grid_list[k].maxX_bd;
   maxY_bd=mediapage.grid_list[k].maxY_bd;
   minX_bd=mediapage.grid_list[k].minX_bd;
   minY_bd=mediapage.grid_list[k].minY_bd;
   cellcolor=mediapage.grid_list[k].defaultRGB;
   var pStart = new BMap.Point(maxX_bd,maxY_bd);
   var pEnd = new BMap.Point(minX_bd,minY_bd);
   var rectangle = new BMap.Polygon([
    new BMap.Point(pStart.lng,pStart.lat),
    new BMap.Point(pEnd.lng,pStart.lat),
    new BMap.Point(pEnd.lng,pEnd.lat),
    new BMap.Point(pStart.lng,pEnd.lat)
    ], {strokeWeight:0.000001, strokeOpacity:0.00000001,
     fillColor:cellcolor});
   rectangleAll.push(rectangle);

   rectangleAll[k].find=mediapage.grid_list[k].find;
   rectangleAll[k].addEventListener('click', function() {
    if (!sessionStorage.getItem('fnid')) {
      sessionStorage.setItem('fnid',this.find);
      console.log(this.find);
      var SinglePARM={
        orderResultId:sessionStorage.getItem('orderResultId'),
        fnid:this.find,
        orderId:sessionStorage.getItem('orderId'),
        cityCode:sessionStorage.getItem('cityCode')   
      }
      getData('post',url,SingleCell,SinglePARM,detailsInf,$('body'));
    }
  });
   bmaps.addOverlay(rectangle);
 }
 $('.media_data #c_People').html(mediapage.handlePlan.va.countPeople);
 $('.media_data #mediaNumber').html(mediapage.handlePlan.va.mediaNumber);
 $('.data_item .vaCNum').html(rounding(mediapage.handlePlan.va.vaC,1,1));
 $('.data_item .vaRNum').html(rounding(mediapage.handlePlan.va.vaR,1,1));
 $('.data_item .vaFNum').html(rounding(mediapage.handlePlan.va.vaF,1,1));
 $('.data_item .GRPNum').html(rounding(mediapage.handlePlan.va.vaGRP,1,1));
 $('.data_item .CMPNum').html(rounding(mediapage.handlePlan.va.vaCPM,1,1));
 $('.locatinfo #location').html(mediapage.cityName);
 $('.locatinfo #Describe').html(mediapage.weather.cityDescribe);
 $('.locatinfo #Season').html(mediapage.weather.citySeason);
 $('.locatinfo #nowDate').html(mediapage.weather.nowDate);
 $('.weather .temperature').html(mediapage.weather.temperature);
 $('.weather .cond').html(mediapage.weather.cond);
 $('.weather .wind').html(mediapage.weather.wind);
 $('.weather .qlty').html(mediapage.weather.qlty);
 $('.weather .pm25').html(mediapage.weather.pm25);
 var mediatype = mediapage.handlePlan.media.name;
 var mediafirname=[],mediafirnum=[],mediafirva=[],mediafirvaC=[],mediafirvaF=[],mediafirGRP=[],mediafirvaR=[],mediafirCPM=[],medianame=[];
 var mediastr='',mediastr2='',mediastr3='';
 for(var k in mediapage.handlePlan.media){
   mediafirname.push(mediapage.handlePlan.media[k].name);
   mediafirnum.push(mediapage.handlePlan.media[k].number);
   mediafirvaC.push(rounding(mediapage.handlePlan.media[k].va.vaC,1,1));
   mediafirvaF.push(rounding(mediapage.handlePlan.media[k].va.vaF,1,1));
   mediafirGRP.push(rounding(mediapage.handlePlan.media[k].va.vaGRP,1,1));
   mediafirvaR.push(rounding(mediapage.handlePlan.media[k].va.vaR,1,1));
   mediafirCPM.push(rounding(mediapage.handlePlan.media[k].va.vaCPM,1,1));
   medianame.push(mediapage.handlePlan.media[k].name);
   mediastr2='';
   for(var i in mediapage.handlePlan.media[k].nodes){
    mediastr3='';
    for(var a in mediapage.handlePlan.media[k].nodes[i].nodes){
      mediastr3+='<li>'+mediapage.handlePlan.media[k].nodes[i].nodes[a].name+'<i class="mold mold3">'+mediapage.handlePlan.media[k].nodes[i].nodes[a].number+'</i><span class="mold_trangle "></span></li>'
    }
    mediastr2+='<li><span class="province2">'+mediapage.handlePlan.media[k].nodes[i].name+'</span>'+
    '<ul class="submenu2">'+mediastr3+'</ul></li>'
  }
  mediastr+='<li>'+
  '<div class="province"><i class="addopen"></i>'+mediapage.handlePlan.media[k].name+'<i class="mold mold1">'+mediapage.handlePlan.media[k].number+'</i><span class="mold_trangle "></span></div>'+
  '<ul class="submenu">'+mediastr2+'</ul></li>'
}
$('.media_wrp .accordion').append(mediastr);
var viewsub= function (_id,setid,option,xydata,medianame,mediaata) {
 echarts.dispose(document.getElementById(_id));
 var setid = echarts.init(document.getElementById(_id));
 if (xydata=='x') {
  option.xAxis.data=medianame;
}else if(xydata=='y'){
  option.yAxis.data=medianame;
}
option.series[0].data=mediaata;
setid.setOption(option);
};
var mediamidname=[],mediamidvaC=[],mediamidvaF=[],mediamidGRP=[],mediamidvaR=[],mediamidCPM=[];
var mediasmaname=[],mediasmavaC=[],mediasmavaF=[],mediasmaGRP=[],mediasmavaR=[],mediasmaCPM=[];
for(var k in mediapage.handlePlan.media){
  for(var i in mediapage.handlePlan.media[k].nodes){
   mediamidname.push(mediapage.handlePlan.media[k].nodes[i].name);
   mediamidvaC.push(rounding(mediapage.handlePlan.media[k].nodes[i].va.vaC,1,1));
   mediamidvaF.push(rounding(mediapage.handlePlan.media[k].nodes[i].va.vaF,1,1));
   mediamidGRP.push(rounding(mediapage.handlePlan.media[k].nodes[i].va.vaGRP,1,1));
   mediamidvaR.push(rounding(mediapage.handlePlan.media[k].nodes[i].va.vaR,1,1));
   mediamidCPM.push(rounding(mediapage.handlePlan.media[k].nodes[i].va.vaCPM,1,1));
   for(var a in mediapage.handlePlan.media[k].nodes[i].nodes){
    mediasmaname.push(mediapage.handlePlan.media[k].nodes[i].nodes[a].name);
    mediasmavaC.push(rounding(mediapage.handlePlan.media[k].nodes[i].nodes[a].va.vaC,1,1));
    mediasmavaF.push(rounding(mediapage.handlePlan.media[k].nodes[i].nodes[a].va.vaF,1,1));
    mediasmaGRP.push(rounding(mediapage.handlePlan.media[k].nodes[i].nodes[a].va.vaGRP,1,1));
    mediasmavaR.push(rounding(mediapage.handlePlan.media[k].nodes[i].nodes[a].va.vaR,1,1));
    mediasmaCPM.push(rounding(mediapage.handlePlan.media[k].nodes[i].nodes[a].va.vaCPM,1,1));
  }
}
}
var vaCBtnTimes=0,vaRBtnTimes=0,vaFBtnTimes=0,GRPBtnTimes=0,CPMBtnTimes=0;
$('#vaCBtn').on('click',function(){
  vaCBtnTimes++;
  if (vaCBtnTimes==2) {
    viewsub('mediaechart1',mediaechart1,mediaoption1,'x',mediasmaname,mediasmavaC);
    $(this).html('返回<i class="sublisticon"></i>');
  }else if(vaCBtnTimes==1){
    viewsub('mediaechart1',mediaechart1,mediaoption1,'x',mediamidname,mediamidvaC);
  }else if (vaCBtnTimes==3) {
    vaCBtnTimes=0;
    $(this).html('查看子表<i class="sublisticon"></i>');
    viewsub('mediaechart1',mediaechart1,mediaoption1,'x',mediafirname,mediafirvaC);
  }
});
$('#vaRBtn').on('click',function(){
  vaRBtnTimes++;
  if (vaRBtnTimes==2) {
   viewsub('mediaechart2',mediaechart2,mediaoption2,'x',mediasmaname,mediasmavaR);
   $(this).html('返回<i class="sublisticon"></i>');
 }else if(vaRBtnTimes==1){
   viewsub('mediaechart2',mediaechart2,mediaoption2,'x',mediamidname,mediamidvaR);
 }else if (vaRBtnTimes==3) {
  vaRBtnTimes=0;
  $(this).html('查看子表<i class="sublisticon"></i>');
  viewsub('mediaechart2',mediaechart2,mediaoption2,'x',mediafirname,mediafirvaR);
}
});
$('#vaFBtn').on('click',function(){
  vaFBtnTimes++;
  if (vaFBtnTimes==2) {
   viewsub('mediaechartR1',mediaechartR1,mediaoptionR1,'x',mediasmaname,mediasmavaF);
   $(this).html('返回<i class="sublisticon"></i>');
 }else if(vaFBtnTimes==1){
   viewsub('mediaechartR1',mediaechartR1,mediaoptionR1,'x',mediamidname,mediamidvaF);
 }else if (vaFBtnTimes==3) {
  viewsub('mediaechartR1',mediaechartR1,mediaoptionR1,'x',mediafirname,mediafirvaF);
  $(this).html('查看子表<i class="sublisticon"></i>');
  vaFBtnTimes=0;
}
});
$('#GRPBtn').on('click',function(){
  GRPBtnTimes++;
  if (GRPBtnTimes==2) {
   viewsub('mediaechartR2',mediaechartR2,mediaoptionR2,'y',mediasmaname,mediasmaGRP);
   $(this).html('返回<i class="sublisticon"></i>');
 }else if(GRPBtnTimes==1){
   viewsub('mediaechartR2',mediaechartR2,mediaoptionR2,'y',mediamidname,mediamidGRP);
 }else if (GRPBtnTimes==3) {
  viewsub('mediaechartR2',mediaechartR2,mediaoptionR2,'y',mediafirname,mediafirGRP);
  $(this).html('查看子表<i class="sublisticon"></i>');
  GRPBtnTimes=0;
}
});
$('#CPMBtn').on('click',function(){
  CPMBtnTimes++;
  if (CPMBtnTimes==2) {
   viewsub('mediaechartR3',mediaechartR3,mediaoptionR3,'y',mediasmaname,mediasmaCPM);
   $(this).html('返回<i class="sublisticon"></i>');
 }else if(CPMBtnTimes==1){
   viewsub('mediaechartR3',mediaechartR3,mediaoptionR3,'y',mediamidname,mediamidCPM);
 }else if (CPMBtnTimes==3) {
  viewsub('mediaechartR3',mediaechartR3,mediaoptionR3,'y',mediafirname,mediafirCPM);
  $(this).html('查看子表<i class="sublisticon"></i>');
  CPMBtnTimes=0;
}
});
var mediaechart1 = echarts.init(document.getElementById('mediaechart1'));
var mediaoption1 = {
  tooltip: {},
  grid:{
   x:65,
 },
 xAxis: {
   data:  mediafirname,
   axisTick:{
    show:false
  },
  axisLabel:{
    textStyle: {
     color: '#fff',
     fontStyle: 'normal',
     fontFamily: '微软雅黑',
     fontSize: 12,
   }
 },
 textStyle: {
  color:'#fff',
}
},
yAxis: {
 show:true,
 name:'有效接触人次(千人)',
 nameTextStyle:{
  color:'#fff'
},
axisLabel:{
  textStyle: {
   color: '#3f4462',
   fontStyle: 'normal',
   fontFamily: '微软雅黑',
   fontSize: 12,
 }
},
splitLine: {
  show:false
},
axisTick:{
  show:false
}
},
series: [{        		
 type: 'bar',
 data: mediafirvaC,
 barWidth: '15',
 markLine : {
  data : [
  {type : 'average', name: '平均值'},
  ],

},
itemStyle:{
  normal:{
   barBorderRadius:[10,10,0,0],
   color:'#5a9ff8'
 },

},
label:{
  normal:{
   show: true,
   position: 'top'
 }
}
}]
};

mediaechart1.setOption(mediaoption1);
var mediaechart2 = echarts.init(document.getElementById('mediaechart2'));
var mediaoption2 = {
  grid:{
   x:65,
 },
 color:['#fff'],
 tooltip: {},
 textStyle:{
   color:['#fff'],
 },
 xAxis: {
   data:  mediafirname,
   axisTick:{
    show:false
  }
},
yAxis: {
 show:true,
 name:'有效到达率(%)',

 axisLabel:{
  textStyle: {
   color: '#3f4462',
   fontStyle: 'normal',
   fontFamily: '微软雅黑',
   fontSize: 12,
 }
},
splitLine: {
  show:false
},

textStyle:{
  color:'#3f4462',
},

axisTick:{
  show:false
}
},
series: [{
 type: 'bar',
 data: mediafirvaR,
 barWidth: '15',
 markLine : {
  data : [
  {type : 'average', name: '平均值'}
  ]
},
itemStyle:{
  normal:{
   barBorderRadius:[10,10,0,0],
 }
},
label:{
  normal:{
   show: true,
   position: 'top',
 }
}
}]
};
mediaechart2.setOption(mediaoption2);

var mediaechartR1 = echarts.init(document.getElementById('mediaechartR1'));
var mediaoptionR1 = {
 grid:{
  x:65,
},
xAxis: {
  data: mediafirname,
  axisTick:{
   show:false
 },
 axisLabel:{
   textStyle: {
    color: '#fff',
    fontStyle: 'normal',
    fontFamily: '微软雅黑',
    fontSize: 12,
  }
},
textStyle: {
 color:'#fff',
}
},
yAxis: {
  show:true,
  name:'有效接触人次(千人)',
  nameTextStyle:{
   color:'#fff'
 },
 axisLabel:{
   textStyle: {
    color: '#3f4462',
    fontStyle: 'normal',
    fontFamily: '微软雅黑',
    fontSize: 12,
  }
},
splitLine: {
 show:false
},
axisTick:{
 show:false
}
},
series: [
{

  type: 'line',
  data: mediafirvaF,
  markLine : {
   data : [
   {type : 'average', name: '平均值'}
   ]
 },
 label:{
   normal:{
    show: true,
    position: 'top'
  }
},
itemStyle:{
 normal:{
  barBorderRadius:[10,10,0,0],
  color:'#04f1e9'
},

}
}
]
};
mediaechartR1.setOption(mediaoptionR1);
var mediaechartR2 = echarts.init(document.getElementById('mediaechartR2'));
var mediaoptionR2 = {
  tooltip: {},
  grid:{
   x:65,
 },
 legend: {
   data:  mediafirname,
 },
 xAxis: {
   show:true,
   type:'value',

   axisLabel:{
    textStyle: {
     color: '#fff',
     fontStyle: 'normal',
     fontFamily: '微软雅黑',
     fontSize: 12,
   }
 },
 splitLine: {
  show:false
},
axisTick:{
  show:false
}
},
yAxis: {
 data:  mediafirname,
 name:'vaGRP',
 nameTextStyle:{
   color:'#fff'
 },
 axisTick:{
   show:false
 },
 axisLabel:{
   textStyle: {
    color: '#fff',
    fontStyle: 'normal',
    fontFamily: '微软雅黑',
    fontSize: 12,
  }
},
textStyle: {
 color:'#fff',
}

},
series: [
{                      
 type: 'bar',
 data: mediafirGRP,
 barWidth: '15',
 markLine : {
   data : [
   {type : 'average', name: '平均值'},
   ],

 },
 itemStyle:{
   normal: {
    barBorderRadius:[0,10,10,0],
    color: function(params) {
     var colorList = [
     '#6f4ed2','#ffffff','#269ee4','#00beb8'
     ];
     return colorList[params.dataIndex%4]
   }
 }
},
label:{
 normal:{
  show: true,
  position: 'right'
}
}
}
]
};
mediaechartR2.setOption(mediaoptionR2);
var mediaechartR3 = echarts.init(document.getElementById('mediaechartR3'));
var mediaoptionR3= {
  tooltip: {},
  legend: {
   data:mediafirname,
   align: 'left',
 },
 xAxis: {
   show:true,
   splitNumber:1,
   axisLabel:{
    textStyle: {
     color: '#fff',
     fontStyle: 'normal',
     fontFamily: '微软雅黑',
     fontSize: 12,
   }
 },
 splitLine: {
  show:false
},
axisTick:{
  show:false
}
},
grid:{
 x:65,
},
yAxis: {
 data:  mediafirname,
 name:'千人成本（元）',
 nameTextStyle:{
  color:'#fff'
},
axisTick:{
  show:false
},
axisLabel:{
  textStyle: {
   color: '#fff',
   fontStyle: 'normal',
   fontFamily: '微软雅黑',
   fontSize: 12,
 }
},
textStyle: {
  color:'#fff',
}

},
series: [{                      
 type: 'bar',
 data: mediafirCPM,
 barWidth: '15',
 markLine : {
  data : [
  {type : 'average', name: '平均值'},

  ],

},
itemStyle:{
  normal: {
   barBorderRadius:[0,10,10,0],
   color: function(params) {
    var colorList = [
    '#ffffff','#3fabe3','#2f55be','#6f4ed2'
    ];
    return colorList[params.dataIndex%4]
  }
}

},
label:{
 normal:{
  show: true,
  position: 'right'
}
}
}]
};
mediaechartR3.setOption(mediaoptionR3);
var Accordion = function(el, multiple) {
 this.el = el || {};
 this.multiple = multiple || false;
 var links = this.el.find('.province');
 var link = this.el.find('.province2');
 links.on('click', {el: this.el, multiple: this.multiple}, this.dropdown);
 link.on('click', {el: this.el, multiple: this.multiple}, this.dropdown);
}
Accordion.prototype.dropdown = function(e) {
 var $el = e.data.el;
 $this = $(this),
 $next = $this.next();
 $next.stop().slideToggle();
 var index= $this.parent().index();
 $this.parent().toggleClass(index);
 $this.toggleClass("downicon");

}
var accordion = new Accordion($('.accordion'), false);
$('.maskRight').outerHeight($('.maskLeft').outerHeight());
}
var SinglePARM={
  orderResultId:sessionStorage.getItem('orderResultId'),
  fnid:sessionStorage.getItem('fnid'),
  orderId:sessionStorage.getItem('orderId'),
  cityCode:sessionStorage.getItem('cityCode')   
}
$(function(){
//  var importInf = {
//   orderId:sessionStorage.getItem('orderId'),
//   cityCode:sessionStorage.getItem('cityCode')
// }
if (sessionStorage.getItem('fnid')) {

  getData('post',url,SingleCell,SinglePARM,detailsInf,$('body'));
}else{
  getData('post',url,details,SinglePARM,detailsInf,$('body'));
}
$('.back').on('click','.planBack',function () {
    sessionStorage.removeItem('fnid');
 
    getData('post',url,details,SinglePARM,detailsInf,$('body'));

  })

})